package com.configure.elasticsearch;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by cuich on 2018/2/12.
 */
@Configuration
public class ElasticsearchConfigure {

    @Autowired
    private ElasticsearchProperty properties;


    @Bean(value = "esClient",destroyMethod = "close")
    @ConditionalOnClass(value ={RestHighLevelClient.class})
    @ConditionalOnMissingBean(value ={RestHighLevelClient.class})
    public RestHighLevelClient client(){

        HttpHost[] hosts = properties.getHosts().stream().map(v -> new HttpHost(v.getHostname(),v.getPort())).toArray(HttpHost[]::new);

        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(hosts));

        return client;
    }





}
